package com.yuexun.beilunpatient.ui.registration.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.yuexun.beilunpatient.R;
import com.yuexun.beilunpatient.base.BaseEntity;
import com.yuexun.beilunpatient.base.BaseKJActivity;
import com.yuexun.beilunpatient.config.AppConfig;
import com.yuexun.beilunpatient.ui.registration.adapter.RegistrationDepAdapter;
import com.yuexun.beilunpatient.ui.registration.bean.AppointmentDepBean;
import com.yuexun.beilunpatient.ui.registration.bean.AppointmentDepData;
import com.yuexun.beilunpatient.ui.registration.bean.AppointmentHospitalBean;
import com.yuexun.beilunpatient.ui.registration.model.impl.RegistrationModel;
import com.yuexun.beilunpatient.ui.registration.presenter.impl.AppointmentDepPresenter;
import com.yuexun.beilunpatient.ui.registration.ui.view.IAppointmentDepView;
import com.yuexun.beilunpatient.widget.EmptyLayout;

import org.kymjs.kjframe.ui.ViewInject;
import org.kymjs.kjframe.utils.PreferenceHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by chenss on 2018/10/29.
 */
public class Act_Registration_Department extends BaseKJActivity implements IAppointmentDepView {

    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.center_list)
    ListView centerList;
    @Bind(R.id.center_empty)
    EmptyLayout centerEmpty;
    @Bind(R.id.content_list)
    ListView contentList;
    @Bind(R.id.hos_pic)
    ImageView hosPic;
    @Bind(R.id.hos_name)
    TextView hosName;
    @Bind(R.id.hos_address)
    TextView hosAddress;

    RegistrationDepAdapter adapter;
    RegistrationDepAdapter depAdapter;
    AppointmentDepPresenter depPresenter;
    List<Object> depBeans = new ArrayList<>();
    ArrayList<Object> centerDepBeans = new ArrayList<>();
    AppointmentHospitalBean hosBean;

    @Override
    public void setRootView() {
        setContentView(R.layout.act_registration_department);
        ButterKnife.bind(this);
    }

    @Override
    public void initData() {
        super.initData();
        hosBean = (AppointmentHospitalBean) getIntent().getExtras().getSerializable("hosBean");
//        tvTitle.setText(hosBean.getHospital_name());
        Glide.with(aty).load(hosBean.getHospital_picture()).into(hosPic);
        hosName.setText(hosBean.getHospital_name());
        hosAddress.setText(hosBean.getHospital_address());

        depPresenter = new AppointmentDepPresenter(new RegistrationModel(), this);
        ApiGetDep();

        adapter = new RegistrationDepAdapter(centerList, centerDepBeans, R.layout.item_registrationdep);
        centerList.setAdapter(adapter);
        depAdapter = new RegistrationDepAdapter(contentList, depBeans, R.layout.item_registrationdep);
        contentList.setAdapter(depAdapter);

        centerEmpty.setOnLayoutClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                centerEmpty.setErrorType(EmptyLayout.NETWORK_LOADING);
                ApiGetDep();
            }
        });

        centerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                for(Object depData : centerDepBeans){
                    AppointmentDepData item = (AppointmentDepData) depData;
                    item.setSelect(false);
                }
                AppointmentDepData data = (AppointmentDepData) centerDepBeans.get(i);
                data.setSelect(true);
                adapter.refresh(centerDepBeans);

                depBeans.clear();
                depBeans.addAll(((AppointmentDepData) adapterView.getItemAtPosition(i)).getDepList());
                depAdapter.refresh(depBeans);
            }
        });

        contentList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                AppointmentDepBean item = (AppointmentDepBean) adapterView.getItemAtPosition(i);
                Bundle bundle = new Bundle();
                bundle.putString("depId", item.getDepartment_id());
                bundle.putString("hosId", hosBean.getHospital_id());
                bundle.putString("type", "1");//科室编号  医院-科室-医生
                Intent intent = new Intent(aty, Act_Registration_Doctor.class);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
    }

    private void ApiGetDep() {
        /**
         * mzsj 门诊时间（A上午P下午F全天）(只有resources_type为Y时有效多个组合，例子：A|P|F
         departmentName 科室模糊查询关键字
         scheduleEnddate 排班结束日期yyyy-mm-dd（resources_type为Y时有效）
         scheduleBegdate 排班开始日期yyyy-mm-dd（resources_type为Y时有效）
         resourcesType 资源类型（默认为空，返回所有科室列表，为Y时返回所有可预约科室列表，为D时返回当天有导诊信息的科室列表）
         hospitalId 医院编号（若为空则返回所有中心科室列表，不为空则返回所有该医院科室列表 resourcesType为D时必填）
         */
        Map<String, String> params = new HashMap<>();
        params.put("mzsj", "A|P|F");
        params.put("departmentName", "");
        params.put("scheduleEnddate", "");
        params.put("scheduleBegdate", "");
        params.put("resourcesType", "Y");
        params.put("hospitalId", hosBean.getHospital_id());
        params.put("sessionUuidPatient", PreferenceHelper.readString(aty, AppConfig.USER_INFO, AppConfig.SESSION_ID));
        depPresenter.getAppointentDep(params);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.title_back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_back:
                finish();
                break;
        }
    }

    @Override
    public void getAppointentDep(BaseEntity<AppointmentDepData> baseEntity) {
        if (baseEntity.getStatus().equals("200")) {
            if(baseEntity.getDatas().size() > 0){
                baseEntity.getDatas().get(0).setSelect(true);
                centerDepBeans.addAll(baseEntity.getDatas());
                depBeans.addAll(baseEntity.getDatas().get(0).getDepList());
                adapter.refresh(centerDepBeans);
                depAdapter.refresh(depBeans);
            }
        } else {
            ViewInject.toast(baseEntity.getMessage());
        }

        if (centerDepBeans.size() == 0) {
            centerEmpty.setErrorType(EmptyLayout.NODATA);
        } else {
            centerEmpty.dismiss();
        }
    }
}
